package de.lulusoft.anothertorrent.gui.views;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.progress.UIJob;

import de.lulusoft.anothertorrent.core.servers.Server;
import de.lulusoft.anothertorrent.gui.AnotherTorrentGui;
import de.lulusoft.anothertorrent.gui.preferences.PreferenceConstants;
import de.lulusoft.anothertorrent.gui.util.Util;
import de.lulusoft.anothertorrent.gui.views.Providers.OverviewViewContentProvider;
import de.lulusoft.anothertorrent.gui.views.Providers.OverviewViewLabelProvider;
import de.lulusoft.anothertorrent.gui.views.Providers.TorrentListViewInput;

public class OverviewView extends ViewPart {
	
	public static final String ID = "de.lulusoft.anothertorrent.gui.views.OverviewView";
	
	private TreeViewer viewer = null;
	private Text infoUpRate = null;
	private Text infoDownRate = null;
	private Text infoClientVersion = null;
	private Text infoLibVersion = null;
	
	private Server server = null;
	private Job timer = null;
	

	public OverviewView() {
	}
	
	@Override
	public void dispose() {
		timer.cancel();
		super.dispose();
	}

	public void setInput(Object input)
	{
		if (input instanceof Server)
		{
			timer.cancel();
			this.server = (Server)input;
			setPartName(server.getName());
			viewer.setInput(input);
			timer.schedule();
		}
	}
	
	public void updateServerInfo()
	{
		infoUpRate.setText(Util.formatBytes(server.getUploadRate()));
		infoDownRate.setText(Util.formatBytes(server.getDownloadRate()));
		infoClientVersion.setText(server.getClientVersion());
		infoLibVersion.setText(server.getLibVersion());		
	}

	@Override
	public void createPartControl(Composite parent) {
		
		Composite main = new Composite(parent, SWT.FILL);
		main.setLayout(new GridLayout(2, false));
		
		GridData gd = new GridData();
		gd.grabExcessHorizontalSpace = true;
		gd.verticalAlignment = SWT.FILL;
		gd.horizontalAlignment = SWT.FILL;
		
		Label linfoUpRate = new Label(main, SWT.NONE);
		linfoUpRate.setText("UpRate: ");
		linfoUpRate.setLayoutData(gd);
		infoUpRate = new Text(main, SWT.READ_ONLY);
		infoUpRate.setLayoutData(gd);
		
		Label linfoDownRate = new Label(main, SWT.NONE);
		linfoDownRate.setText("DownRate: ");
		linfoDownRate.setLayoutData(gd);
		infoDownRate = new Text(main, SWT.READ_ONLY);
		infoDownRate.setLayoutData(gd);
		
		Label linfoClientVersion = new Label(main, SWT.NONE);
		linfoClientVersion.setText("Client Version: ");
		linfoClientVersion.setLayoutData(gd);
		infoClientVersion = new Text(main, SWT.READ_ONLY);
		infoClientVersion.setLayoutData(gd);
		
		Label linfoLibVersion = new Label(main, SWT.NONE);
		linfoLibVersion.setText("Lib Version: ");
		linfoLibVersion.setLayoutData(gd);
		infoLibVersion = new Text(main, SWT.READ_ONLY);
		infoLibVersion.setLayoutData(gd);
		
		gd = new GridData();
		gd.grabExcessHorizontalSpace = true;
		gd.grabExcessVerticalSpace = true;
		gd.verticalAlignment = SWT.FILL;
		gd.horizontalAlignment = SWT.FILL;
		gd.horizontalSpan = 2;
		
		
		viewer = new TreeViewer(main, SWT.SINGLE);
		viewer.getTree().setLayoutData(gd);
		viewer.setContentProvider(new OverviewViewContentProvider());
		
		viewer.setLabelProvider(new OverviewViewLabelProvider());
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
		
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				
//				TorrentListView.setInput(event.getSelection());		
				if (event.getSelection() instanceof IStructuredSelection)
				{
					IStructuredSelection sel = (IStructuredSelection)event.getSelection();
					if (sel.getFirstElement() instanceof String)
					{
						String viewName = (String)sel.getFirstElement();
						// Open View
						try 
						{
							TorrentListView ov = (TorrentListView)
								AnotherTorrentGui.getDefault()
								.getWorkbench().getActiveWorkbenchWindow()
								.getActivePage().showView(TorrentListView.ID, 
										server.getName(), IWorkbenchPage.VIEW_ACTIVATE);
							ov.setInput(new TorrentListViewInput(server, viewName));
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}		
					}
				}	
			}
		});
		
		timer = new Job("ServerView Update") {
		
			@Override
			protected IStatus run(IProgressMonitor monitor) {
				monitor.beginTask("ServerView Updating Data", IProgressMonitor.UNKNOWN);
				try 
				{
					
					UIJob updateUI = new UIJob(Display.getDefault(), "ServerView Update GUI") {
					
						@Override
						public IStatus runInUIThread(IProgressMonitor monitor) {
							try
							{
								monitor.beginTask("ServerView Updating GUI", IProgressMonitor.UNKNOWN);
								if (server != null)
									server.updateGlobalStats();
								updateServerInfo();
								monitor.done();
							} catch (Exception e) {
								e.printStackTrace();
							}
							return null;
						}
					};
					updateUI.setUser(false);
					updateUI.schedule();
					timer.schedule(AnotherTorrentGui.getDefault()
							.getPreferenceStore().getInt(PreferenceConstants.P_UPDATE_INTERVAL_SERVER_VIEW) * 1000);
					monitor.done();					
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				return null;
			}
		};
		timer.setUser(false);

	}

	@Override
	public void setFocus() {
	}

}
